package com.service.impl;

import com.bean.Announcement;
import com.mapper.AnnouncementMapper;
import com.service.AnnouncementService;
import com.utils.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnnouncementServiceImpl implements AnnouncementService {
    @Autowired
    private AnnouncementMapper announcementMapper;

    @Override
    public Announcement getAnnouncementById(Integer announcementId) {
        return announcementMapper.getAnnouncementById(announcementId);
    }

    @Override
    public PageInfo<Announcement> getAnnouncementsWithKeyword(String keyword, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<Announcement> announcements = announcementMapper.getAnnouncementsWithKeyword(keyword, offset, pageSize);
        int total = announcementMapper.countAnnouncementsWithKeyword(keyword);
        return new PageInfo<>(pageNum, pageSize, total, announcements);
    }

    @Override
    public boolean insertAnnouncement(Announcement announcement) {
        int result = announcementMapper.insertAnnouncement(announcement);
        return result > 0;
    }

    @Override
    public boolean updateAnnouncement(Announcement announcement) {
        int result = announcementMapper.updateAnnouncement(announcement);
        return result > 0;
    }

    @Override
    public boolean deleteAnnouncementById(Integer announcementId) {
        int result = announcementMapper.deleteAnnouncementById(announcementId);
        return result > 0;
    }
}